package design_master.yin97.com.myapplication;

import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;



public class ToastUtils {
    private static Toast mToast = null;

    /**
     * Toast的初始化
     *
     * @param context 上下文
     */
    public static void init(Context context) {
        if (mToast == null) {
            mToast = Toast.makeText(context, "", Toast.LENGTH_SHORT);
            if (mToast.getView() instanceof LinearLayout) {
                ((TextView) ((LinearLayout) mToast.getView()).getChildAt(0)).setGravity(Gravity.CENTER);
            } else if (mToast.getView() instanceof RelativeLayout) {
                ((TextView) ((RelativeLayout) mToast.getView()).getChildAt(0)).setGravity(Gravity.CENTER);
            }
        }
    }

    /**
     * Toast的显示(默认位置)
     *
     * @param message 需要显示的信息
     */
    public static void showToast(CharSequence message) {
        if (mToast != null && !TextUtils.isEmpty(message)) {
            try {
                mToast.setText(message);
                mToast.show();
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            if (!TextUtils.isEmpty(message)) {
                Toast.makeText(MyAppaction.getInstance(), message, Toast.LENGTH_SHORT).show();
            }
        }
    }
}
